package lanqiao13;

import java.util.Scanner;

public class H {
	private static int n;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		sc.close();
		
		// 如果n是一个质数，返回-1
		if (isPrime(n)) {
			System.out.println(-1);
			return;
		}
		
		int p1 = largestPrimeFactor(n);
		int a1 = n - p1 + 1;
		int ans = Integer.MAX_VALUE;
		for (int i = a1; i <= n; i++) {
			if (isPrime(i)) {
				continue;
			}
			
			int p0 = largestPrimeFactor(i);
			int a0 = i - p0 + 1;
			ans = Math.min(ans, a0);
		}
		
		System.out.println(ans);
	}
	
	
	
	// 判断一个数是否为质数
	private static boolean isPrime(int num) {
		for (int i = 2; i <= Math.sqrt(num); i++) {
			if (num % i == 0) {
				return false;
			}
		}
		
		return true;
	}
	
	// 求一个数的最大质因数
	private static int largestPrimeFactor(int n) {
		if (n <= 1) {
			return -1;
		}
		int largest = 2;
		for (int i = 2; i <= Math.sqrt(n); i++) {
			while (n % i == 0) {
				largest = i;
				n /= i;
			}
		}
		if (n > 1) {
			largest = n;
		}
		return largest;
	}
}
